package com.aaa.jvmtest.methodarea;

/**
 * 演示StringTables垃圾回收
 * -Xmx10m  设置堆内存大小
 * -XX:+PrintStringTableStatistics   打印字符串表的统计信息(个数)
 * -XX:+PrintGCDetails -verbose:gc   发生垃圾回收时，打印次数、时间等信息
 *
 * @author Tu
 * @date 2022/6/7 21:57
 */
public class StringTablesGCManager {

    public static void main(String[] args) {
        //initGC();
        //operate();
        operateGC();
    }

    public static void initGC() {
        //默认对象个数为1767
        int i = 0;
        try {
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            System.out.println(i);
        }
    }

    public static void operate() {
        //对象个数为1869
        int i = 0;
        try {
            for (int j = 0; j < 100; j++) {
                String.valueOf(j).intern();
                i++;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            System.out.println(i);
        }
    }

    public static void operateGC() {
        //对象个数为9951，触发一次GC回收
        int i = 0;
        try {
            for (int j = 0; j < 10000; j++) {
                String.valueOf(j).intern();
                i++;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            System.out.println(i);
        }
    }


    //由于内存空间分配失败，触发执行的GC记录(多次会多条)，清除无用的字符串常量，时间短是因为minerGC新生代，回收很快
    /**
     * [GC (Allocation Failure) [PSYoungGen: 2048K->488K(2560K)] 2048K->755K(9728K), 0.0028303 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
     */
    //添加的-verbose:gc,打印的GC信息
    /**
     * Heap
     *  PSYoungGen      total 2560K, used 2036K [0x00000000ffd00000, 0x0000000100000000, 0x0000000100000000)
     *   eden space 2048K, 99% used [0x00000000ffd00000,0x00000000ffefd030,0x00000000fff00000)
     *   from space 512K, 0% used [0x00000000fff80000,0x00000000fff80000,0x0000000100000000)
     *   to   space 512K, 0% used [0x00000000fff00000,0x00000000fff00000,0x00000000fff80000)
     *  ParOldGen       total 7168K, used 0K [0x00000000ff600000, 0x00000000ffd00000, 0x00000000ffd00000)
     *   object space 7168K, 0% used [0x00000000ff600000,0x00000000ff600000,0x00000000ffd00000)
     *  Metaspace       used 3378K, capacity 4496K, committed 4864K, reserved 1056768K
     *   class space    used 371K, capacity 388K, committed 512K, reserved 1048576K
     */
    //类的符号表，类名、方法名等，也都会被放入到内存中，属于常量池的一部分(理解为字符)
    /**
     * SymbolTable statistics:
     * Number of buckets       :     20011 =    160088 bytes, avg   8.000
     * Number of entries       :     13973 =    335352 bytes, avg  24.000
     * Number of literals      :     13973 =    618832 bytes, avg  44.288
     * Total footprint         :           =   1114272 bytes
     * Average bucket size     :     0.698
     * Variance of bucket size :     0.702
     * Std. dev. of bucket size:     0.838
     * Maximum bucket size     :         6
     */
    //StringTable的信息  底层类似HashTable的实现，Hash表:数组加链表，数组的个数称为桶
    //Number of buckets : 60013  这里表示默认桶个数为60013个
    //Number of entries          字符串对象个数
    //Number of literals         串池中常量个数
    //Total footprint            总占用空间
    /**
     * StringTable statistics:
     * Number of buckets       :     60013 =    480104 bytes, avg   8.000
     * Number of entries       :      1767 =     42408 bytes, avg  24.000
     * Number of literals      :      1767 =    179136 bytes, avg 101.379
     * Total footprint         :           =    701648 bytes
     * Average bucket size     :     0.029
     * Variance of bucket size :     0.030
     * Std. dev. of bucket size:     0.172
     * Maximum bucket size     :         2
     */


}
